package com.fishery.quartz.controller;


import com.fishery.entity.Result;
import com.fishery.quartz.entity.Task;
import com.fishery.quartz.entity.vo.OperateTaskVo;
import com.fishery.quartz.entity.vo.UpdateTaskVo;
import com.fishery.quartz.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ghost
 * @since 2021-02-21
 */
@RestController
@RequestMapping("/quartz")
@Api(tags = "定时任务模块")
@CrossOrigin
public class TaskController {

	@Autowired
	private TaskService taskService;


	@PostMapping("/add")
	@ApiOperation("添加任务")
	public Result addTask(@RequestBody Task task) {
		if (task == null
				|| StringUtils.isEmpty(task.getJobName())
				|| StringUtils.isEmpty(task.getJobName())
				|| StringUtils.isEmpty(task.getCreateName())
				|| StringUtils.isEmpty(task.getCronExpression())) {
			return Result.error("添加任务失败");
		}
		return taskService.createTask(task) ? Result.success("添加任务成功") : Result.error("添加任务失败");
	}

	@GetMapping("/running")
	@ApiOperation("查询正在运行的任务")
	public Result searchRunningTask() {
		return Result.success().data(taskService.getAllTasksByStatus("1"));
	}

	@GetMapping("/running/{size}/{current}")
	@ApiOperation("查询正在运行的任务（带分页）")
	public Result searchRunningTaskWithPage(@PathVariable(name = "current") int current,
											@PathVariable(name = "size") int size) {
		return Result.success().data(taskService.getPageOfAllRunningTasks(current, size));
	}


	@GetMapping("/pause")
	@ApiOperation("查询已经暂停的任务")
	public Result searchPauseTask() {
		return Result.success().data(taskService.getAllTasksByStatus("0"));
	}

	@GetMapping("/pause/{size}/{current}")
	@ApiOperation("查询已经暂停的任务（带分页）")
	public Result searchPauseTaskWithPage(@PathVariable(name = "current") int current,
										  @PathVariable(name = "size") int size) {
		return Result.success().data(taskService.getPageOfAllStopTasks(current, size));
	}


	@PutMapping("/pause")
	@ApiOperation("暂停任务")
	public Result pauseTask(@RequestBody OperateTaskVo vo) {
		if (vo == null
				|| StringUtils.isEmpty(vo.getId())
				|| StringUtils.isEmpty(vo.getJobName())
				|| StringUtils.isEmpty(vo.getJobGroup())
				|| StringUtils.isEmpty(vo.getModifiedName())) {
			return Result.error("暂停任务失败");
		}
		return taskService.stopTask(vo.getId(), vo.getJobName(), vo.getJobGroup(),
				vo.getModifiedName()) ? Result.success("暂停任务成功") : Result.error("暂停任务失败");
	}

	@PutMapping("/resume")
	@ApiOperation("恢复任务")
	public Result resumeTask(@RequestBody OperateTaskVo vo) {
		if (vo == null
				|| StringUtils.isEmpty(vo.getId())
				|| StringUtils.isEmpty(vo.getJobName())
				|| StringUtils.isEmpty(vo.getJobGroup())
				|| StringUtils.isEmpty(vo.getModifiedName())) {
			return Result.error("恢复任务失败");
		}
		return taskService.resumeTask(vo.getId(), vo.getJobName(), vo.getJobGroup(),
				vo.getModifiedName()) ? Result.success("恢复任务成功") : Result.error("恢复任务失败");
	}

	@PutMapping("/update")
	@ApiOperation("修改任务")
	public Result updateTask(@RequestBody UpdateTaskVo vo) {
		if (vo == null
				|| StringUtils.isEmpty(vo.getId())
				|| StringUtils.isEmpty(vo.getModifiedName())) {
			return Result.error("修改失败");
		}
		return taskService.updateParamsOfTask(vo) ? Result.success("修改成功") : Result.error("修改失败");
	}


	@DeleteMapping("/delete")
	@ApiOperation("删除任务")
	public Result deleteTask(@RequestBody OperateTaskVo vo) {
		if (vo == null
				|| StringUtils.isEmpty(vo.getId())
				|| StringUtils.isEmpty(vo.getJobName())
				|| StringUtils.isEmpty(vo.getJobGroup())
				|| StringUtils.isEmpty(vo.getModifiedName())) {
			return Result.error("删除任务失败");
		}
		return taskService.removeTask(vo.getId(), vo.getJobName(),
				vo.getJobGroup(), vo.getModifiedName()) ? Result.success("删除任务成功") : Result.error("删除任务失败");
	}

}

